home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / src.lha / src / amitcp / net / sana2copybuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-08  |  7.6 KB  |  319 lines

  1. RCS_ID_C="$Id: sana2copybuff.c,v 1.15 1993/12/20 18:06:41 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * sana2copybuff.c - Buffer Management Routines for Sana-II Interfaces
  8.  *
  9.  * Last modified: Sun Nov  7 01:37:49 1993 ppessi
  10.  *
  11.  * HISTORY
  12.  * $Log: sana2copybuff.c,v $
  13.  * Revision 1.15  1993/12/20  18:06:41  jraja
  14.  * Added more robust version of ioip_alloc_mbuf().
  15.  * Added more detail to "mbuf chain short" message.
  16.  *
  17.  * Revision 1.13  1993/11/06  23:39:15  ppessi
  18.  * Added more information to "mbuf chain short" error message.
  19.  *
  20.  * Revision 1.12  1993/06/04  11:16:15  jraja
  21.  * Fixes for first public release.
  22.  *
  23.  * Revision 1.11  1993/05/16  21:09:43  ppessi
  24.  * RCS version changed.
  25.  *
  26.  * Revision 1.10  1993/05/05  16:10:38  puhuri
  27.  * Fixed cluster allocation code.
  28.  *
  29.  * Revision 1.9  93/04/26  11:53:11  11:53:11  too (Tomi Ollila)
  30.  * Changed include paths of amiga_api.h, amiga_libcallentry.h and amiga_raf.h
  31.  * from kern to api
  32.  * 
  33.  * Revision 1.8  93/04/24  22:46:13  22:46:13  jraja (Jarno Tapio Rajahalme)
  34.  * Removed Define for USECLUSTERS
  35.  * 
  36.  * Revision 1.7  93/04/13  22:22:56  22:22:56  jraja (Jarno Tapio Rajahalme)
  37.  * Changed return from buffer allocation function back to recent form.
  38.  * 
  39.  * Revision 1.6  93/04/12  09:20:40  09:20:40  jraja (Jarno Tapio Rajahalme)
  40.  * Changed reserved mbuf chain so that all mbufs after the header have 
  41.  * clusters.
  42.  * 
  43.  * Revision 1.5  93/04/05  17:46:26  17:46:26  jraja (Jarno Tapio Rajahalme)
  44.  * Changed spl storage variables to spl_t.
  45.  * Changed every .c file to use conf.h.
  46.  * 
  47.  * Revision 1.4  93/03/20  07:11:55  07:11:55  ppessi (Pekka Pessi)
  48.  * Fixed mbuf allocating for headers
  49.  * 
  50.  * Revision 1.3  93/03/05  19:51:16  19:51:16  jraja (Jarno Tapio Rajahalme)
  51.  * Fixed includes (again).
  52.  * 
  53.  * Revision 1.2  93/02/28  22:21:57  22:21:57  ppessi (Pekka Pessi)
  54.  * Made to compile; used RAFn macros.
  55.  * 
  56.  * Revision 1.1  93/02/25  14:34:29  14:34:29  ppessi (Pekka Pessi)
  57.  * Initial revision
  58.  */
  59.  
  60. #include <conf.h>
  61.  
  62. #include <sys/param.h>
  63. #include <sys/systm.h>
  64. #include <sys/malloc.h>
  65. #include <sys/mbuf.h>
  66. #include <sys/socket.h>
  67. #include <sys/socketvar.h>
  68. #include <sys/syslog.h>
  69. #include <sys/synch.h>
  70.  
  71. #include <net/if.h>
  72.  
  73. #if INET
  74. #include <netinet/in.h>
  75. #include <netinet/in_systm.h>
  76. #include <netinet/in_var.h>
  77. #include <netinet/ip.h>
  78. #endif
  79.  
  80. #include <net/if_sana.h>
  81. #include <api/amiga_raf.h>
  82.  
  83. /*
  84.  * allocate mbufs for the size MTU at free_chain for read request
  85.  */
  86. #if 0 /* old one */
  87. BOOL
  88. ioip_alloc_mbuf(struct IOIPReq *s2rp, ULONG MTU)
  89. {
  90.   register struct mbuf *m, *n;
  91.   register int len = 0;
  92.  
  93.   n = s2rp->ioip_reserved;
  94.  
  95.   /* Check for packet header */
  96.   if (n && (n->m_flags & M_PKTHDR)) {
  97.     /* There is already a full packet  */
  98.     return TRUE;
  99.   }
  100.  
  101.   /* Prepend by a packet header */
  102.   MGETHDR(m, M_NOWAIT, MT_HEADER);
  103.   if (m) { 
  104.     m->m_len = len = MHLEN;
  105.     s2rp->ioip_reserved = m;
  106.     m->m_next = n;
  107.     
  108.     /* Find the end of the free chain */ /* ASSUME THAT THESE HAVE CLUSTERS */
  109.     while (n = m->m_next) {
  110.       len += n->m_len; m = n;
  111.     } 
  112.     
  113.     /*
  114.      * add new (cluster)mbufs to get the desired size
  115.      */
  116.     while (len < MTU) {
  117.       MGET(n, M_NOWAIT, MT_DATA);
  118.       if (n != NULL) {
  119.     MCLGET(n, M_NOWAIT);
  120.     if (n->m_ext.ext_buf != NULL) {
  121.       n->m_len = n->m_ext.ext_size;
  122.       len += n->m_ext.ext_size;
  123.     }
  124.     else {
  125.       m_free(n);
  126.       break;
  127.     }
  128.     m = m->m_next = n;
  129.       }
  130.       else
  131.     break;
  132.     }
  133.     
  134.     s2rp->ioip_reserved->m_pkthdr.len = len;
  135.   }
  136.   if (len < MTU) { 
  137.     m_freem(s2rp->ioip_reserved);
  138.     s2rp->ioip_reserved = NULL;
  139.     return FALSE;
  140.   }
  141.  
  142.   return TRUE;
  143. }
  144. #else
  145. BOOL
  146. ioip_alloc_mbuf(struct IOIPReq *s2rp, ULONG MTU)
  147. {
  148.   register struct mbuf *m, *n;
  149.   register int len;
  150.  
  151.   /*
  152.    * s2rp->ioip_reserved is either NULL, or an mbuf chain, possibly having
  153.    * packet header in the first one.
  154.    */
  155.   n = s2rp->ioip_reserved;
  156.  
  157.   /* Check for packet header */
  158.   if (n && (n->m_flags & M_PKTHDR)) {
  159.     /*
  160.      * chain already has the packet header
  161.      */
  162.     m = n;
  163.     len = m->m_len;
  164.   }
  165.   else {
  166.     /* Prepend by a packet header */
  167.     MGETHDR(m, M_NOWAIT, MT_HEADER);
  168.     if (m) { 
  169.       m->m_len = len = MHLEN;
  170.       s2rp->ioip_reserved = m;
  171.       m->m_next = n;
  172.     }
  173.     else
  174.       goto fail;
  175.   }
  176.   /*
  177.    * Now m points to the start of the mbuf chain. The first mbuf has
  178.    * a packet header. 
  179.    */
  180.  
  181.   /* Find the end of the free chain */ /* ASSUME THAT THESE HAVE CLUSTERS */
  182.   while (n = m->m_next) {
  183.     len += n->m_len; m = n;
  184.   } 
  185.     
  186.   /*
  187.    * Now len has the total length of the mbuf chain, add new 
  188.    * (cluster)mbufs to get the desired size (MTU).
  189.    */
  190.   while (len < MTU) {
  191.     MGET(n, M_NOWAIT, MT_DATA);
  192.     if (n != NULL) {
  193.       MCLGET(n, M_NOWAIT);
  194.       if (n->m_ext.ext_buf != NULL) {
  195.     len += n->m_len = n->m_ext.ext_size;
  196.       }
  197.       else {
  198.     m_free(n);
  199.         goto fail;
  200.       }
  201.       m = m->m_next = n;
  202.     }
  203.     else
  204.       goto fail;
  205.   }
  206.   s2rp->ioip_reserved->m_pkthdr.len = len;
  207.   return TRUE;
  208.  
  209.  fail:
  210.   m_freem(s2rp->ioip_reserved);
  211.   s2rp->ioip_reserved = NULL;
  212.   return FALSE;
  213. }
  214. #endif
  215.  
  216. /*
  217.  * Copy data from an mbuf chain starting from the beginning,
  218.  * continuing for "n" bytes, into the indicated continuous buffer.
  219.  *
  220.  * NOTE: this WILL be called from INTERRUPTS, so compile with stack checking
  221.  *       disabled and use __saveds if near data is needed.
  222.  */
  223. static SAVEDS BOOL RAF3(m_copy_from_mbuf,
  224.          BYTE*,          to,   a0,
  225.          struct IOIPReq*,from, a1,
  226.          ULONG,          n,    d0)
  227. #if 0
  228. {
  229. #endif
  230.   register struct mbuf *m = from->ioip_packet;
  231.   register unsigned count;
  232.  
  233.   while (n > 0) {
  234. #if DIAGNOSTIC
  235.     if (m == 0) {
  236.       log(LOG_ERR, "m_copy_from_buff: mbuf chain short");
  237.       return FALSE;
  238.     }
  239. #endif
  240.     count = MIN(m->m_len, n);
  241.     bcopy(mtod(m, caddr_t), to, count);
  242.     n -= count;
  243.     to += count;
  244.     m = m->m_next;
  245.   }
  246.   return TRUE;
  247. }
  248.  
  249. /*
  250.  * Copy data from an continuous buffer 'from' to preallocated mbuf chain
  251.  * starting from the beginning, continuing for "n" bytes.
  252.  * Mbufs in the preallocated chain must have their m_len field set to maximum
  253.  * amount of data that they can have.
  254.  * 
  255.  * NOTE: this WILL be called from INTERRUPTS, so compile with stack checking
  256.  *       disabled and use __saveds if near data is needed.
  257.  */
  258. static SAVEDS BOOL RAF3(m_copy_to_mbuf,
  259.          struct IOIPReq*,to,   a0,
  260.          BYTE*,          from, a1,
  261.          ULONG,          n,    d0)
  262. #if 0
  263. {
  264. #endif
  265.   register struct mbuf *f, *m = to->ioip_reserved;
  266.   unsigned totlen = n;
  267.  
  268. #if DIAGNOSTIC
  269.   if (!(m->m_flags & M_PKTHDR)) {
  270.     log(LOG_ERR, "m_copy_to_buff: mbuf chain has no header");
  271.     return FALSE;
  272.   }
  273. #endif
  274.  
  275.   while (n > 0) {
  276. #if DIAGNOSTIC
  277.     if (m == 0) {
  278.       log(LOG_ERR, "m_copy_to_buff: mbuf chain short, "
  279.       "packet len =%lu, reserved =%lu, "
  280.       "wiretype =%lu, mtu =%lu",
  281.       totlen, to->ioip_reserved->m_pkthdr.len,
  282.       to->ioip_s2.ios2_PacketType,
  283.       (ULONG)to->ioip_if->ss_if.if_mtu);
  284.       return FALSE;
  285.     }
  286. #endif
  287.     if (n < m->m_len)
  288.       m->m_len = n;
  289.     bcopy(from, mtod(m, caddr_t), m->m_len);
  290.     from += m->m_len;
  291.     n -= m->m_len;
  292.     if (n > 0)
  293.       m = m->m_next;
  294.   }
  295.  
  296.   /*
  297.    * move the packet to the field 'ioip_packet',
  298.    * set total length of the packet and terminate it.
  299.    */
  300.   f = m->m_next;        /* first free mbuf */
  301.   m->m_next = NULL;        /* terminate the chain */
  302.  
  303.   to->ioip_packet = to->ioip_reserved;
  304.   to->ioip_packet->m_pkthdr.len = totlen; /* set packet length */
  305.   to->ioip_reserved = f;        /* leftover mbufs */
  306.  
  307.   /*
  308.    * More mbuf flags and interface pointer must be set later
  309.    */
  310.   return TRUE;
  311. }
  312.  
  313. struct TagItem buffermanagement[3] = {
  314.     { S2_CopyToBuff,   (ULONG)m_copy_to_mbuf },
  315.     { S2_CopyFromBuff, (ULONG)m_copy_from_mbuf },
  316.     { TAG_END, }
  317. };
  318.  
  319.